package cn.smilefox.dao.impl;

import cn.smilefox.dao.TeacherDao;
import cn.smilefox.domain.Teacher;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.PreparedStatementCreator;
import org.springframework.jdbc.support.GeneratedKeyHolder;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.List;

public class TeacherDaoImpl implements TeacherDao {

    private JdbcTemplate jdbcTemplate;
    public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
        this.jdbcTemplate = jdbcTemplate;
    }

    @Override
    public List<Teacher> findall() {
        final List<Teacher> teacherList = jdbcTemplate.query("select * from teacher", new BeanPropertyRowMapper<Teacher>(Teacher.class));
        System.out.println(teacherList);
        return teacherList;
    }

    @Override
    public Long save(final Teacher teacher) {

        //获取保存数据的id
        //创建PreparedStatementCreator
        PreparedStatementCreator creator = new PreparedStatementCreator() {
            @Override
            public PreparedStatement createPreparedStatement(Connection connection) throws SQLException {
                //用原始的jdbc去完成一个PreparedStatement组件
                PreparedStatement preparedStatement = connection.prepareStatement("insert into teacher values (?,?,?,?,?,?)",PreparedStatement.RETURN_GENERATED_KEYS);
                preparedStatement.setObject(1,null);
                preparedStatement.setObject(2,teacher.getTc_name());
                preparedStatement.setString(3,teacher.getSex());
                preparedStatement.setString(4,teacher.getLearn());
                preparedStatement.setString(5,teacher.getStudy());
                preparedStatement.setString(6,teacher.getPhone_num());
                return preparedStatement;
            }
        };

        //创建keyHolder
        GeneratedKeyHolder keyHolder = new GeneratedKeyHolder();

        //获取主键
        jdbcTemplate.update(creator, keyHolder);
        long id = keyHolder.getKey().longValue();
//        jdbcTemplate.update("insert into teacher values (?,?,?,?,?,?)",null,teacher.getTc_name(),teacher.getSex(),teacher.getLearn(),teacher.getStudy(),teacher.getPhone_num());
        return id;
    }

    @Override
    public void saveTeacherRoleRel(Long id, Long[] roleIds) {
        for (Long roleId : roleIds) {
            jdbcTemplate.update("insert into rela_teacher_role values (?,?)",id,roleId);
        }

    }

    @Override
    public void delTeacherRoleRel(Long teacherId) {
        jdbcTemplate.update("delete from rela_teacher_role where teacher_id=?",teacherId);
    }

    @Override
    public void del(Long teacherId) {
        jdbcTemplate.update("delete from teacher where id=?",teacherId);
    }

    @Override
    public List<Teacher> find(Teacher teacher) {
        List<Teacher> teacherList = jdbcTemplate.query("select * from teacher where tc_name=?",new BeanPropertyRowMapper<Teacher>(Teacher.class),teacher.getTc_name());
        System.out.println(teacherList);
        return teacherList;
    }

    @Override
    public List<Teacher> findTeacherById(Long id) {

        List<Teacher> teachers = jdbcTemplate.query("SELECT * FROM rela_teacher_test tt,teacher t WHERE tt.teacher_id=t.id AND tt.testc_id=?;", new BeanPropertyRowMapper<Teacher>(Teacher.class), id);
        System.out.println(teachers);
        return teachers;
    }

    //理论课查找老师
    @Override
    public List<Teacher> findTeacherById2(Long id) {
        List<Teacher> teachers = jdbcTemplate.query("SELECT * FROM rela_teacher_theory tt,teacher t WHERE tt.teacher_id=t.id AND tt.theoryc_id=?;", new BeanPropertyRowMapper<Teacher>(Teacher.class), id);
        System.out.println(teachers);
        return teachers;
    }


}
